﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Organizer.ViewModel;
using Organizer.Model;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Navigation;
using GalaSoft.MvvmLight.Command;

namespace Organizer.ViewModel
{
    public class TaskEditPageVM : BaseViewModel
    {
        Task _task; //объект, который приняли для редактирования
        Task _tempTask; //временный объект-копия
        bool _isEdit;
        Guid _taskId;

        public ICommand ActionIconTappedCommand
        {
            get;
            private set;
        }

        //private void CancelTap()
        //{
        //    NavigationService.Navigate(new Uri("/TaskList", UriKind.Relative));
        //}

        private void OnActionIconTapped()
        {
            MessageBox.Show("Action icon tapped.");
        }

        public void Save()
        {
            if (IsEdit)
            {
                _tempTask.Assign(_task);
                OrganizerDataContext.Instance.SubmitChanges();
            }
            else
            {
                _task = _tempTask;
                OrganizerDataContext.Instance.Tasks.InsertOnSubmit(_task);
                OrganizerDataContext.Instance.SubmitChanges();
            }
        }

        public bool IsEdit
        {
            get { return _isEdit; }
        }

        public TaskEditPageVM()
        {
            this.ActionIconTappedCommand = new RelayCommand(this.OnActionIconTapped);
        }

        public TaskEditPageVM(Guid editTaskId)
            : this()
        {
            _taskId = editTaskId;

            if (editTaskId == Guid.Empty)
            {
                _isEdit = false;
                _tempTask = new Task();
            }
            else
            {
                _task = OrganizerDataContext.Instance.GetTaskById(_taskId);
                if (_task != null)
                {
                    _isEdit = true;
                    _tempTask = new Task();
                    _task.Assign(_tempTask);
                }
            }


        }

        public Guid TaskId
        {
            get { return _taskId; }
        }

        public String Name
        {
            get { return _tempTask.Name; }
            set { _tempTask.Name = value; }
        }

        public String Description
        {
            get { return _tempTask.Description; }
            set { _tempTask.Description = value; }
        }

        public DateTime TimeCreate
        {
            get { return _tempTask.TimeCreate; }
        }

        public DateTime Timestart
        {
            get { return _tempTask.TimeStart; }
            set { _tempTask.TimeStart = value; }
        }

        public DateTime TimeEnd
        {
            get { return _tempTask.TimeEnd; }
            set { _tempTask.TimeEnd = value; }
        }

        public List<Man> ManList
        {
            get
            {
                return OrganizerDataContext.Instance.Mans.ToList();
            }
        }

        public Man LinkedMan
        {
            get { return _tempTask.LinkedMan; }
            set { _tempTask.LinkedMan = value; }
        }

        public Man ResponsibleMan
        {
            get { return _tempTask.ResponsibleMan; }
            set { _tempTask.ResponsibleMan = value; }
        }
    }
}
